<template>
  <div class="exchange_wrap">
    <headTop :headClass="'white_header'">return point record</headTop>
    <div class="record_wrap">
      <div class="record_title">
        <span class="details">details</span>
        <div class="date_wrap" @click="selectDate">
          <span>{{showDate}}</span>
          <i class="iconfont icon-rili"></i>
        </div>
      </div>
      <div class="scroll_wrap" @scroll="toggleHead" ref="dataBox">
        <div class="public_none_data" v-if="!recordList.length && isRequest">No data</div>
        <ul class="record_list">
          <li class="record_item" v-for="(item, index) in recordList" :key="index">
            <div class="left_wrap">
              <span class="remark">{{item.typeName}}</span>
              <span class="time">{{item.create_at}}</span>
            </div>
            <div class="left_wrap">
              <span class="remark money">{{item.money}}<span class="time">Return point</span></span>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'exchangeRecord',
  data () {
    return {
      recordList: [],
      isRequest: false, // Have you requested data?
      canRequest: true, // Can you request data, request lock?
      page: 1, // Paging default data
      hasMoreData: true // Do you have more data?
    };
  },
  beforeMount () {
    this.getData();
  },
  watch: {
    showDate () {
      this.page = 1;
      this.isRequest = false;
      this.hasMoreData = true;
      this.recordList = [];
      this.getData();
    }
  },
  computed: {
    showDate () {
      if (!this.$store.state.date.selectResult[1]) {
        return 'This month';
      } else {
        return `${this.$store.state.date.selectResult[0]}-${this.$store.state.date.selectResult[1]}`;
      }
    },
    startTime () {
      if (!this.$store.state.date.selectResult[1]) {
        return `${new Date().getFullYear()}-${new Date().getMonth() + 1}`;
      } else {
        return this.$store.state.date.selectResult.join('-');
      }
    },
    endTime () {
      if (!this.$store.state.date.selectResult[1]) {
        return `${new Date().getFullYear()}-${new Date().getMonth() + 2}`;
      } else {
        return `${this.$store.state.date.selectResult[0]}-${this.$store.state.date.selectResult[1] + 1}`;
      }
    }
  },
  methods: {
    // Rolling events
    toggleHead (e) {
      let withBottomScroll = document.querySelector('.record_list').clientHeight - (this.$refs.dataBox.clientHeight + this.$refs.dataBox.scrollTop);
      if (withBottomScroll < 50 && this.canRequest && this.hasMoreData) {
        this.getData();
      }
    },
    selectDate () {
      this.$store.commit('configDateWrap');
    },
    // Request record
    getData () {
      this.canRequest = false;
      this.axios.post('index/accountlog/present_point_exchange', {
        page: this.page,
        time: this.startTime,
        type: 1
      }).then(({data}) => {
        this.isRequest = true;
        if (data.code === 200) {
          this.canRequest = true;
          this.page++;
          // Array obtained
          let resObj = data.data;
          if (resObj.length < 10 || !resObj.length || !resObj) {
            this.hasMoreData = false;
          }
          this.recordList = this.recordList.concat(resObj);
        } else {
          this.Toast(data.message);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
  @import '../style/public.scss';
  .exchange_wrap{
    width: 100%;
    padding-top: 110px;
    background-color: #fff;
    height: 100%;
    .white_header{
      white-space: nowrap;
    }
    .record_wrap{
      width: 100%;
      height: 100%;
      background-color: #fff;
      position: relative;
      .record_title{
        width: 100%;
        height: 96px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 30px;
        border-bottom: 1px solid #e6e6e6;
        .details{
          font-size: 28px;
          color: #666;
        }
        .date_wrap{
          display: flex;
          align-items: center;
          font-size: 24px;
          color: #666;
          .iconfont{
            font-size: 30px;
            color: #282828;
            margin-left: 16px;
          }
        }
      }
      .scroll_wrap{
        width: 100%;
        height: calc(100% - 96px);
        overflow-y: auto;
        position: relative;
        .record_list{
          padding: 0 28px;
          .record_item{
            width: 100%;
            padding: 40px 0;
            border-bottom: 1px solid #e6e6e6;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .left_wrap{
              display: flex;
              flex-direction: column;
              font-size: 28px;
              color: #282828;
              .remark{
                margin-bottom: 14px;
              }
              .remark.money{
                color: $color;
              }
              .time{
                font-size: 26px;
                color: #999;
              }
            }
          }
        }
      }
      &::before{
        display: block;
        content: '';
        width: 100%;
        height: 20px;
        background-color: #f5f5f5;
        position: absolute;
        left: 0;
        top: -20px;
      }
    }
  }
</style>
